package com.gz.qqserveer;
import com.gz.common.Message;
import com.gz.common.MessageType;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

public class ServerConnectClientThread extends Thread {
        private Socket socket;
        private String userId;

    public ServerConnectClientThread(Socket socket, String userId) {
        this.socket = socket;
        this.userId = userId;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void run() {
        while (true){

            try {
                System.out.println("服务器端口与客户端 "+userId+" 连接成功");
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) ois.readObject();

                if(message.getMestype().equals(MessageType.MESSAGE_GET_ONLINE_USER)){
                    System.out.println(message.getSender()+" 要在线列表");
                    String onlineUser=MangeServerConnectClientThread.getOnlineUser();
                    //返回在线列表
                    //构建一个Message返回给客户端
                    Message message2=new Message();
                    message2.setContent(onlineUser);
                    message2.setMestype(MessageType.MESSAGE_RET_ONLINE_USER);
                    message2.setReceiver(message.getSender());
                    //返回给客户端
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                    objectOutputStream.writeObject(message2);
                }
                else if(message.getMestype().equals(MessageType.MESSAGE_CLIENT_EXIT)){
                    System.out.println(message.getSender()+" 退出线程");
                    MangeServerConnectClientThread.removeServerConnectClientThread(message.getSender());
                    QQserver.getOfflineMessages().put(message.getSender(),new ArrayList<Message>());
                    socket.close();
                    break;
                } else if (message.getMestype().equals(MessageType.MESSAGE_COMM_MES)) {
                   if(MangeServerConnectClientThread.getHashMap().containsKey(message.getReceiver())){
                       System.out.println("(服务端) "+message.getSendTime()+"\t"+message.getSender()+"对"+message.getReceiver()+"说:"
                               +message.getContent());
                       ObjectOutputStream oops = new ObjectOutputStream(MangeServerConnectClientThread.getServerConnectClientThread(
                               message.getReceiver()).getSocket().getOutputStream());
                       oops.writeObject(message);
                   }
                   else{
                       System.out.println("(服务端) "+message.getSendTime()+"\t"+message.getSender()+"对"+message.getReceiver()+"发送离线消息: "
                               +message.getContent());
                       if(QQserver.getOfflineMessages().containsKey(message.getReceiver())){
                           QQserver.getOfflineMessages().get(message.getReceiver()).add(message);
                       }
                       else{
                           QQserver.getOfflineMessages().put(message.getReceiver(),new ArrayList<Message>());
                           QQserver.getOfflineMessages().get(message.getReceiver()).add(message);
                       }
                   }

                } else if (message.getMestype().equals(MessageType.MESSAGE_TO_ALL_MES)) {
                    System.out.println("(服务端) "+message.getSendTime()+" "+message.getSender()+" 对大家说(群消息): "+message.getContent());
                    HashMap<String,ServerConnectClientThread> hashMap=MangeServerConnectClientThread.getHashMap();
//                    Collection<ServerConnectClientThread> values = hashMap.values();
//                   for (ServerConnectClientThread serverConnectClientThread : values) {
//                       ObjectOutputStream oops = new ObjectOutputStream(serverConnectClientThread.getSocket().getOutputStream());
//                       oops.writeObject(message);
//                   }
                    Iterator<String>iterator=hashMap.keySet().iterator();
                    while(iterator.hasNext()){//遍历客户端socket，给除了发送端的在线用户发送群发消息
                        String userId=iterator.next();
                        if(!userId.equals(message.getSender())){
                            ObjectOutputStream oops = new ObjectOutputStream(hashMap.get(userId).getSocket().getOutputStream());
                            oops.writeObject(message);
                        }
                    }

                } else if (message.getMestype().equals(MessageType.MESSAGE_FILE_MES)) {
                    if(MangeServerConnectClientThread.getHashMap().containsKey(message.getReceiver())){
                        System.out.println(message.getSendTime()+" "+message.getSender()+"向"+message.getReceiver()
                                +"发送文件");
                        ObjectOutputStream oops = new ObjectOutputStream(MangeServerConnectClientThread.getServerConnectClientThread(
                                message.getReceiver()).getSocket().getOutputStream());
                        oops.writeObject(message);
                    }

                    else{
                        System.out.println("(服务端) "+message.getSendTime()+"\t"+message.getSender()+"对"+message.getReceiver()+"发送离线文件: "
                                +message.getContent());
                        if(QQserver.getOfflineMessages().containsKey(message.getReceiver())){
                            QQserver.getOfflineMessages().get(message.getReceiver()).add(message);
                        }
                        else{
                            QQserver.getOfflineMessages().put(message.getReceiver(),new ArrayList<Message>());
                            QQserver.getOfflineMessages().get(message.getReceiver()).add(message);
                        }
                    }


                } else{
                    System.out.println("是其他类型的message，暂时不处理");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
